package com.winsdom.controller.CustomerManagementController;

import com.winsdom.anno.Authentication;
import com.winsdom.entity.customer.*;
import com.winsdom.service.customer.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

import static com.winsdom.enums.AuthenticationType.CUSTOMER_MANAGEMENT;

/**
 * 客户基础信息管理控制器
 * 提供企业、个体户和个人客户的基础信息CRUD操作
 * 客户类型枚举：1-企业客户，2-个体工商户，3-个人客户
 */
@RestController
@Api(tags = "客户基础信息管理", description = "企业、个体户和个人客户的基础信息管理接口")
@Authentication(value=CUSTOMER_MANAGEMENT)
public class BasicInfoController {

    @Resource
    private CustomerEnterpriseBasicInfoService customerEnterpriseBasicInfoService;

    /**
     * 获取企业基础信息
     */
    @PostMapping("/customer/enterprise/basicInfo")
    @ApiOperation(value = "获取企业基础信息", notes = "根据客户ID返回完整企业信息")
    public CustomerEnterpriseBasicInfoResponse getEnterpriseBasicInfo(@RequestBody Map<String, Long> request) {
        Long customerId = request.get("customerId");
        return customerEnterpriseBasicInfoService.selectEnterpriseBasicInfo(customerId);
    }

    @Resource
    private CustomerEnterpriseUpdateService customerEnterpriseUpdateService;

    /**
     * 更新企业基础信息
     */
    @PostMapping("/customer/enterprise/updateBasicInfo")
    @ApiOperation(value = "更新企业基础信息", notes = "支持部分字段更新，customerId为必填项")
    public ResponseResult<String> updateEnterpriseBasicInfo(@RequestBody CustomerEnterpriseUpdateRequest request) {
       customerEnterpriseUpdateService.updateEnterpriseBasicInfo(request);
        return ResponseResult.success("企业客户信息更新成功");
    }

    @Autowired
    private CustomerEnterpriseCreateService enterpriseCreateService;

    /**
     * 创建企业客户
     */
    @PostMapping("/customer/enterprise/create")
    @ApiOperation(value = "创建企业客户", notes = "所有字段均为必填，需符合格式要求")
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功，返回customerId", response = Long.class),
            @ApiResponse(code = 400, message = "参数缺失或格式错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    public ResponseResult<Long> createEnterprise(@RequestBody CustomerEnterpriseCreateRequest createInfo) {
        try {
            Long customerId = enterpriseCreateService.createEnterprise(createInfo);
            return ResponseResult.success(customerId);
        } catch (Exception e) {
            return ResponseResult.error("创建企业失败: " + e.getMessage());
        }
    }

    // ---------------------- 个体户接口 ---------------------- //

    @Resource
    private CustomerIndividualBasicInfoService customerIndividualBasicInfoService;

    /**
     * 获取个体户基础信息
     */
    @PostMapping("/customer/basicInfo")
    @ApiOperation(value = "获取个体户基础信息", notes = "返回个体户完整信息")
    public CustomerIndividualBasicInfoRequest getCustomerBasicInfo(@RequestBody Map<String, Long> request) {
        Long customerId = request.get("customerId");
        return customerIndividualBasicInfoService.getCustomerBasicInfo(customerId);
    }

    @Resource
    private CustomerIndividualUpdateService individualService;

    /**
     * 更新个体户信息
     */
    @PostMapping("/customer/individual/update")
    @ApiOperation(value = "更新个体户信息", notes = "customerId为必填，其他字段可部分更新")
    public ResponseResult<String> updateIndividualInfo(
            @Valid @RequestBody CustomerIndividualUpdateRequest request
    ) {
        individualService.update(request);
        return ResponseResult.success("个体户信息更新成功");
    }

    @Resource
    private CustomerIndividualCreateService service;

    /**
     * 创建个体户客户
     */
    @PostMapping("/customer/individual/create")
    @ApiOperation(value = "创建个体户客户", notes = "所有字段均为必填，需符合格式要求")
    public ResponseResult<String> createIndividualCustomer(@Valid @RequestBody CustomerIndividualCreateRequest request) {
        service.create(request);
        return ResponseResult.success("个体户创建成功");
    }

    // ---------------------- 个人接口 ---------------------- //

    @Resource
    private CustomerPersonalBasicInfoService customerPersonalBasicInfoService;

    /**
     * 获取个人客户信息
     */
    @PostMapping("/customer/personal/basicInfo")
    @ApiOperation(value = "获取个人客户信息", notes = "返回个人完整信息")
    public CustomerPersonalBasicInfoRequest getCustomerPersonalBasicInfo(@RequestBody Map<String, Object> request) {
        Long customerId = Long.valueOf(request.get("customerId").toString());
        return customerPersonalBasicInfoService.getCustomerPersonalBasicInfo(customerId);
    }

    @Resource
    private CustomerPersonalUpdateService customerPersonalUpdateService;

    /**
     * 更新个人客户信息
     */
    @PostMapping("/customer/personal/update")
    @ApiOperation(value = "更新个人客户信息", notes = "customerId为必填，其他字段可部分更新")
    public ResponseResult<String> updatePersonalCustomer(@Valid @RequestBody CustomerPersonalUpdateRequest request) {
        customerPersonalUpdateService.update(request);
        return ResponseResult.success("个人客户信息更新成功");
    }

    @Resource
    private CustomerPersonalCreateService createService;

    /**
     * 创建个人客户信息
     */
    @PostMapping("/customer/personal/create")
    @ApiOperation(value = "创建个人客户信息", notes = "所有字段均为必填，需符合格式要求")
    public ResponseResult<String> createCustomer(@RequestBody CustomerPersonalCreateRequest request) {
        createService.create(request);
        return ResponseResult.success("个人客户信息创建成功");
    }
}